package cn.ml.utils.shiJianChuo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class CountDown {

    public static long mss = 60 * 60 * 24;// 21599

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");
        Scanner input = new Scanner(System.in);
        System.out.println("请手动输入时间，格式为：yyyy-MM-dd");
        String next = input.next();
        //String s= "2022-01-01"; //手动设置元旦时间
        //Date date = sdf.parse(s);
        Date date = sdf.parse(next);//输入日期转化为国际日期
        long time = date.getTime();//国际日期转化为计算机日期


        //开始循环倒计时
        while (mss > 0){
            long timeNow = System.currentTimeMillis();// new Date()为获取当前系统时间，也可使用当前时间戳
            mss = (time - timeNow)/1000;
            if (mss < 0){
                System.err.println("当天日期已过去！");
                break;
            }
            mss--;
            try {
                Thread.sleep(1000);
                long days = mss / (60 * 60 * 24);
                long hours = (mss % (60 * 60 * 24)) / (60 * 60);
                long minutes = (mss % (60 * 60)) / (60);
                long seconds = (mss % 60);

                if (days == 0 && hours == 0 && minutes == 0 && seconds == 0){
                    System.out.println("恭喜倒计时完成！！！");
                    break;
                }
                System.out.println("还剩" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}
